﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormContactDetail : Form
    {
        protected SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        protected SqlCommand cmd;
        protected SqlDataAdapter sda;
        protected DataSet ds;
        protected int id;
        public FormContactDetail(int id)
        {
            this.id = id;
            InitializeComponent();
        }

        private void FormContactDetail_Load(object sender, EventArgs e)
        {
            FillGroup();
            txtId.Text = id.ToString();
            //string sql = string.Format("select * from contact where id = '{0}'", id.ToString());
            cmd = new SqlCommand("GetContactById");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                txtName.Text = sdr["name"].ToString();
                txtPhone.Text = sdr["phone"].ToString();
                txtQQ.Text = sdr["qq"].ToString();
                txtEmail.Text = sdr["email"].ToString();
                txtWorkPhone.Text = sdr["officephone"].ToString();
                txtWorkAddress.Text = sdr["workunit"].ToString();
                txtHomePhone.Text = sdr["homephone"].ToString();
                txtHomeAddress.Text = sdr["homeaddress"].ToString();
                txtMemo.Text = sdr["memo"].ToString();
                cboGroup.SelectedValue = sdr["groupid"].ToString();
            }
            sdr.Close();
            conn.Close();
        }
        
        protected void FillGroup()
        {
            //string sql = "select * from contactgroup order by id desc";
            cmd = new SqlCommand("GetAllContactGroup",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            sda = new SqlDataAdapter(cmd);
            ds = new DataSet();
            sda.Fill(ds);
            cboGroup.DisplayMember = "groupname";
            cboGroup.ValueMember = "id";
            cboGroup.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq = txtQQ.Text.Trim();
            string workAddress = txtWorkAddress.Text.Trim();
            string workPhone = txtWorkPhone.Text.Trim();
            string homeAddress = txtHomeAddress.Text.Trim();
            string homePhone = txtHomePhone.Text.Trim();
            string memo = txtMemo.Text.Trim();
            int groupId = (int)cboGroup.SelectedValue;
            if (!Check(name, phone, email, qq, workPhone, homePhone))
                return;
            cmd = new SqlCommand("UpdateContact", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@phone", phone);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@qq", qq);
            cmd.Parameters.AddWithValue("@officeaddress", workAddress);
            cmd.Parameters.AddWithValue("@officephone", workPhone);
            cmd.Parameters.AddWithValue("@homeaddress", homeAddress);
            cmd.Parameters.AddWithValue("@homePhone", homePhone);
            cmd.Parameters.AddWithValue("@memo", memo);
            cmd.Parameters.AddWithValue("@groupid", groupId);
            cmd.Parameters.AddWithValue("@id", id);
            conn.Open();
            //string sql = string.Format("update contact set name = '{0}',phone = '{1}',email = '{2}',qq='{3}',workunit='{4}',officephone='{5}',homeaddress='{6}',homephone='{7}',memo='{8}',groupid='{9}' where id='{10}'",
            //    name,phone,email,qq,workAddress,workPhone,
            //    homeAddress,homePhone,memo,groupId,id);
            //cmd = new SqlCommand(sql, conn);
            int n = cmd.ExecuteNonQuery();
            if (n == 1)
            {
                MessageBox.Show("更新联系人信息成功。");
            }
            else
            {
                MessageBox.Show("更新记录失败。");
            }
            conn.Close();
            this.Close();
        }

        bool Check(string name, string phone, string email, string qq, string officephone, string homephone)
        {
            if (name == "")
            {
                MessageBox.Show("联系人姓名不能为空。");
                txtName.Focus();
                return false;
            }
            if (!Utility.CheckMobilePhone(phone))
            {
                MessageBox.Show("手机号码格式不正确。");
                txtPhone.Focus();
                return false;
            }
            if (!Utility.CheckMail(email))
            {
                MessageBox.Show("邮箱地址格式错误！");
                txtEmail.Focus();
                return false;
            }
            if (!Utility.CheckQQ(qq))
            {
                MessageBox.Show("QQ号码不正确！");
                txtQQ.Focus();
                return false;
            }
            if (!Utility.CheckPhone(officephone))
            {
                MessageBox.Show("办公电话不正确！");
                txtWorkPhone.Focus();
                return false;
            }
            if (!Utility.CheckPhone(homephone))
            {
                MessageBox.Show("家庭电话不正确。");
                txtHomePhone.Focus();
                return false;
            }
            return true;
        }
    }
}
